home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 11 / Commodore_Free_Issue_11_2007_Commodore_Computer_Club.d64 / t.hexfiles 6 < prev    next >
File List  |  2023-02-26  |  8KB  |  270 lines

  1. u           Hexfiles part 6
  2.             By Jason Kelk
  3.    http://www.oldschoolgaming.com/
  4.  
  5. Hello children, today we're going to be
  6. looing at raster interrupts & later on
  7. we're going to be making one of our
  8. own. You'll need the inside of a toilet
  9. roll, some sticky backed plastic & 2
  10. washing up liquid bottles remember to
  11. use plasticine behind card when you
  12. want to make a hole with your scissors
  13. & you may want the help of a
  14. responsible adult when cutting things.
  15. Or at least an irresponsible adult,
  16. since they're more fun. Before I start
  17. a word of warning; the first piece of
  18. example code in this article uses
  19. stroboscopic effects & some viewers may
  20. find these effects at the least
  21. uncomfortable to watch.
  22.  
  23. If you have a history of epilepsy or an
  24. adversion to strobe effects, don't
  25. execute the first piece of code, just
  26. read the documentation & dissection.
  27. This only applies to the file called
  28. rasterD1.asm & the later files can be
  29. executed with no difficulty. Last
  30. issue I left you all messing around
  31. with the sine curves on that little
  32. demo we did, didn't I? So, did you
  33. enjoy yourselves? Good! Okay, now up
  34. until now our code has been using
  35. what's called "runtime", & in theory
  36. anything can be written that way. But
  37. the C64 can also do something called
  38. an interrupt which, as the name
  39. suggests, involves interrupting the
  40. runtime code & popping off to do some-
  41. thing else. Normally the C64 interrupt
  42. is set to a part of the Kernel ROM (the
  43. set of housekeeping code that C=
  44. supplied with the machine) & it takes
  45. care of reading the keyboard & a few
  46. other little tasks. Since we believe in
  47. leading by example I won't just go off
  48. & teach you about all the different
  49. kinds of interrupt, for now the only
  50. one you need is the raster interrupt.
  51. If you remember our demo, it waited for
  52. a raster line using $D012 & then did
  53. something, but using a raster interrupt
  54. we can do something else on the runtime
  55. & whenever that raster position comes
  56. around, the machine interrupts what the
  57. runtime code is doing & performs a few
  58. other tasks. Okay, so we need some code
  59. to look at to get this concept across,
  60.  
  61. Load Turbo Assembler (it's on this
  62. issue's cover disk) & the source file
  63. rasterD1.asm As usual I'll do a break-
  64. down of what you've just loaded in a
  65. second, but for now assemble & run it
  66. with SYS2304. The code appears to have
  67. run but but the cursor has come back up
  68. again & the border is flashing very
  69. rapidly, right? That's what we mean by
  70. interrupt, the C64 is stopping the run-
  71. time code (where BASIC & our previous
  72. pieces of code work from) once a frame
  73. & is doing a little INC $D020 to flash
  74. the border colour. And we can do any-
  75. thing from interrupt, play music, move
  76. sprites, scroll the screen & with more
  77. than one split, change what the VIC
  78. chip is doing at various points down
  79. the screen. Okay, so lets take a look
  80. at our code:
  81.  
  82. split = $00
  83.  
  84. * = $0900 sei First off, we set up
  85. split as a label, giving it an actual
  86. value of $00. If we change that value
  87. we can alter
  88.  
  89. where the raster actually takes place
  90. on the screen, from line $00 (as here)
  91. to line $FF. And, as before, we set
  92. our code start with the *, this time
  93. to $0900 (2304). That's followed by a
  94. quick SEI to turn off the interrupts,
  95. so that nothing goes amiss while we
  96. change things.
  97.  
  98. lda #<int
  99. sta $0314
  100. lda #>int
  101. sta $0315
  102.  
  103. $0314 and $0315 are where the C64
  104. looks to find the address it should
  105. call for the interrupt, normally it's
  106. set to $EA31 (the housekeeping routines
  107. we mentioned earlier) but we want it
  108. pointing to our code called int, which
  109. will later call $EA31 itself. The LDA
  110. #< and LDA #> will get the memory
  111. location of int for us, saving us from
  112. working it out; in this case, $0314
  113. contains $27 & $0315 gets $40, since
  114. INT gets put at $4027 by the assembler.
  115. This reverse the numbers system is
  116. called lowbyte/highbyte format (since
  117. the lowbyte, the $27 here, goes first &
  118. the highbyte goes second) & from here
  119. onwards, reference to the lowbyte will
  120. mean the last two digits of a hex
  121. number & highbyte will mean the first
  122. two.
  123.  
  124. lda #$7f
  125. sta $dc0d
  126. sta $dd0d
  127.  
  128. Despite the interrupts being "halted"
  129. by the SEI command earlier, the
  130. interrupt flags themselves can still
  131. be set; writing $7F over $DC0D &
  132. clearing the top bit prevents a rogue
  133. IRQ interrupt occurring whilst doing
  134. the same to $DD0D prevents rogue NMI
  135. interrups. The latter are less common
  136. but more likely the longer the program
  137. spends in "SEI mode" so this is a nice
  138. little precaution to prevent some hard
  139. to track erratic crashes that may occur
  140. as your code becomes more complex.
  141.  
  142. lda #split
  143. sta $d012
  144. lda #$1b
  145. sta $d011
  146.  
  147. Now we set the position of the split
  148. we want by writing it to $D012, the
  149. raster register. In this case it's
  150. defined by the label split, as we've
  151. previously mentioned, so it will be
  152. positioned on rasterline $00 which is
  153. at the top of the screen. The reason
  154. we set $D011 is to clear the Most
  155. Significant Bit (or MSB) of the raster,
  156. since there are over 256 raster lines
  157. on the screen (on PAL machines, as used
  158. in the U.K. & Europe) a single $00 to
  159. $FF range is too small so one of the
  160. bits of $D011 is used as a ninth bit to
  161. give a total range of $000 to $1ff.
  162.  
  163. lda #$01
  164. sta $d019
  165. sta $d01a
  166.  
  167. $D019 is an indicator, the first bit
  168. tells us when an interrupt has
  169. happened, so we're just priming it here
  170. ready for the main routine & then
  171. writing to $D01A in order to tell the
  172. C64 that we want a raster interrupt,
  173. not any of the other kinds it has
  174. available.
  175.  
  176. cli rts
  177.  
  178. Ah, now CLI is a new command. It's
  179. pretty much the reverse of SEI, it
  180. turns all the interrupts back on &
  181. therefore enables our new interrupt.
  182. Then it's an RTS to take us back to
  183. BASIC, at least for the runtime code, &
  184. that's the interrupt setup out of the
  185. way! Now it's time to get onto what
  186. happens during the interrupt itself.
  187.  
  188. int lda $d019
  189.  
  190. and #$01
  191.  
  192. sta $d019
  193.  
  194. int is the routine we pointed $0314 &
  195. $0315 to earlier, so it's now where the
  196. C64 looks when a raster interrupt is
  197. called. At this point, we want to check
  198. $D019 to see if the first bit is 0 or 1
  199. (in other words, if the Most Signifi-
  200. cant Bit of the raster is set, since
  201. $D012 can be $00 twice during the
  202. screen & we want the one where the MSB
  203. isn't set) and..
  204.  
  205. bne dosplit
  206. jmp $ea81
  207.  
  208. ..if it's the former we can go on to
  209. our raster split, the routine dosplit
  210. (or "do split"). Otherwise we call
  211. $EA81, which is another of the little
  212. housekeeping routines from the C64 ROM
  213. that does most of what our house-
  214. keeping friend $EA31 does but without
  215. the keyboard reading. After all, we
  216. don't want the keyboard read more than
  217. once every 50th of a second, it causes
  218. all sorts of problems!
  219.  
  220. dosplit
  221. lda #split
  222. sta $d012
  223.  
  224. Since the value we put into $D012 gets
  225. cleaned out when the interrupt is
  226. triggered and a split happens, we have
  227. to put it back in again for the next
  228. frame.
  229.  
  230. inc $d020
  231.  
  232. Then we increment the border colour,
  233. just to make what's happening a little
  234. more noticable (otherwise it would look
  235. exactly the same as a normal system
  236. interrupt).
  237.  
  238. jmp $ea31 And to round things off we
  239. call $EA31 which, as I've already
  240. mentioned, is where the C64 normally
  241. goes on it's own interrupts (the values
  242. in $0314 and $0315 are $31 & $EA
  243. respectively) so that the housekeeping
  244. happens.
  245.  
  246. Well, that's pretty much your lot
  247. again, but just for something to play
  248. with until I get back next issue, load
  249. rasterD2.asm, then assemble the code &
  250. run it. Most of the code & data is from
  251. the original demo which we've covered
  252. quite extensively before now but a few
  253. slight changes have been made. The most
  254. major are that it now runs using a
  255. raster interrupt and the settings for
  256. $D018 & the addition of a custom
  257. character set but I'll explain the
  258. latter in more detail next time. Once
  259. again, if you have any queries,
  260. comments or suggestions contact me &
  261. we'll do lunch. The source code for the
  262. routines above can be downloaded here
  263. for easier reference
  264.  
  265. http://www.oldschoolgaming.com/
  266. files/c64/hexDfiles/pa rtD6Dfiles.zip
  267.  
  268. Copyright Oldschool gaming Reprinted
  269. with permission of Jason Kelk
  270.